<html>
<head>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8">
	<title>Extjs api gen</title>
	<link rel="stylesheet" type="text/css" href="ext2/resources/css/ext-all.css" />
	<script type="text/javascript" src="ext2/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="ext2/ext-core-debug.js"></script>
	
	<!--<script type="text/javascript" src="ext2/ext-lang-zh_CN.js"></script>-->
	<link rel="stylesheet" type="text/css" href="js/liujing.css" />
	<script type="text/javascript" src="js/liujing.js"></script>
	<style>
	.lj-column {
		display:inline;
	}
	.liujing-form-field{margin:0;font:normal 12px tahoma,arial,helvetica,sans-serif;}
	</style>
	<script>
	Ext.BLANK_IMAGE_URL = 'ext2/resources/images/default/s.gif';
	
	Ext.onReady(
		function(){
						
			var inputClass=new Ext.form.TextField({value:'Ext.BoxComponent',width:160});
			inputClass.on('specialkey',function(target,event){
					if(event.getKey()==event.ENTER){
						
						submitFun();
					}
			});
			
			var inputIsStatic=new Ext.form.ComboBox({
					mode: 'local',
					store:new Ext.data.SimpleStore({
						fields:['a','b'],
						data:[
							['new Object','n'],['static','s'],['prototype','p']
						]}),
					forceSelection:true,
					editable:false,
					selectOnFocus:true,
					triggerAction:'all',
					displayField:'a',
					valueField:'b'
					,value:'n'
				}
			);
			
			var layerInquiry=new liujing.Panel({height:30});
			//var layerInquiry=new Ext.Panel({layout:'column'});
			var button=new Ext.Button({text:'OK',
					handler:function (){
						submitFun()
					}
						
			});
			
			
			layerInquiry.add(inputClass);
			layerInquiry.add(inputIsStatic);
			layerInquiry.add(button);
			//inputClass.render(Ext.get('d01'));
			//inputIsStatic.render(Ext.get('d02'));
			//new liujing.Label(button).render(Ext.get('d03'));
			
			
			layerInquiry.render(Ext.getBody());
			var p1=new Ext.Panel(
				{title:'Component',frame:true,collapsible: true}
			);
			var p1a=new liujing.Label();
			p1.add(p1a);
			var p2=new liujing.Label();
			p1.render(Ext.getBody());
			p2.render(Ext.getBody());
			var submitFun=function(){
				var a=new ApiGen(eval(inputClass.getValue()),inputIsStatic.getValue());
				p1a.setHTML(a.report());
				p2.getEl().dom.innerHTML="";
				a.renderDetail(p2.getEl());
			};
	});
	
	function ApiMember(sName,ref,type,bOverride){
		this.name=sName;
		this.ref=ref;
		this.type=type;
		this.isOverride=bOverride;
	}
	function ApiGen(constructor,instanceType)
	{
		this.fields=[];
		this.functions=[];
		this.superClass=null;
		this.superApiGen=null;
		this.hasRead=false;
		this.theClass=constructor;
		this.instanceType=instanceType;
		
		this.sortFunc=function(mem1,mem2){
			if(mem1.name<mem2.name) return -1;
			else if(mem1.name==mem2.name) return 0;
			else return 1;
		};
		
		this.reportSuper=function(){
			if(this.superApiGen!=null)
				return this.superApiGen.report();
			else
				return "nothing";
		};
		this.indexOfMember=function(sName){
			//console.debug("?has "+sName);
			for(var i=0,l=this.fields.length;i<l;i++){
				if(this.fields[i].name==sName){
					return i;
				}
			}
			for(var i=0,l=this.functions.length;i<l;i++){
				if(this.functions[i].name==sName){
					return i;
				}
			}
			
			return -1;
		};
		
		//alert(Ext.type(this.theClass));
		
	}
	//ApiGen.prototype.extendFields=[];
	ApiGen.prototype.report=function(){
			this.read();
			if(this.instanceType!='s')
				this.checkExtend();
			var s=["<ul><h1>Fields:</h1>"];
			for(var i=0,l=this.fields.length;i<l;i++){
				var m=this.fields[i];
				if(!m.isOverride){
					s.push('<li><a href="#'+m.name+'">');
					s.push(m.name);
					s.push(' - ');
					s.push(m.type);
					s.push('</a>');
				}
			}
			s.push('</ul>');
			
			s.push ("<h1>Fields extend:</h1>");
			for(var i=0,l=this.fields.length;i<l;i++){
				var m=this.fields[i];
				if(m.isOverride){
					s.push('');
					s.push(m.name);
					s.push(' : (');
					s.push(m.type);
					s.push('), ');
				}
			}
			
			s.push('<br><ul><h2>Methods:</h2>');
			for(var i=0,l=this.functions.length;i<l;i++){
				var m=this.functions[i];
				if(!m.isOverride){
					s.push('<li><a href="#'+m.name+'">');
					s.push(m.name);
					s.push('</a>');
				}
			}
			s.push('</ul>');
			s.push ("<h2>Methods extend:</h2>");
			for(var i=0,l=this.functions.length;i<l;i++){
				var m=this.functions[i];
				if(m.isOverride){
					s.push('<a href="#'+m.name+'">');
					s.push(m.name);
					s.push('</a>, ');
				}
			}
			return s.join('');
		};
	
	ApiGen.prototype.read=function(){
		if(this.hasRead) return;
		this.hasRead=true;
		var p;
		if(this.instanceType=='s'){
			p=this.theClass;
		}else if(this.instanceType=='n'){
			p=new this.theClass();
		}else if(this.instanceType=='p'){
			p=this.theClass.prototype;
		}
		
		if(this.superApiGen!=null)
			this.superApiGen.read();
		for(var mem in p){
			var type=Ext.type(p[mem]);
			if(type=='function'){
				this.functions.push(new ApiMember(mem,p[mem],type,false));
			}
			else{
				this.fields.push(new ApiMember(mem,p[mem],type,false));
			}
		}
		
		this.fields.sort(this.sortFunc);
		this.functions.sort(this.sortFunc);
		if(Ext.isIE){
				this.functions.unshift(new ApiMember('constructor',this.theClass,'function',false));
		}
	};
	ApiGen.prototype.checkExtend=function(){
		if(this.theClass.superclass){
			this.superClass=this.theClass.superclass;
			if(Ext.type(this.superClass)){
				this.superApiGen=new ApiGen(this.superClass.constructor,this.instanceType);
			}
		}
		if(this.superApiGen!=null){
			this.superApiGen.read();
			for(var i=0,l=this.fields.length;i<l;i++){
				var idx=this.superApiGen.indexOfMember(this.fields[i].name);
				if(idx>=0){
					
					var o=this.fields[i];
					o.isOverride=true;
				}
			}
			for(var i=0,l=this.functions.length;i<l;i++){
				var idx=this.superApiGen.indexOfMember(this.functions[i].name);
				if(idx>=0
					&& this.superApiGen.functions[idx].ref==this.functions[i].ref){
					var o=this.functions[i];
					o.isOverride=true;
				}
			}
		}
	};
	ApiGen.prototype.renderDetail=function(containerEl)
	{
		for(var i=0,l=this.functions.length;i<l;i++){
			new liujing.Label('<a name="'+this.functions[i].name+'"></a>').render(containerEl);
			var panel=new Ext.Panel({title:this.functions[i].name, frame:true});
			
			var preEl=new Ext.Element(document.createElement('pre'));
			var functionDetail=this.functions[i].ref.toString();
			//fix IE problem
			if(Ext.isIE)
				functionDetail=functionDetail.replace(/\n/g,'\n\r');
			
			preEl.dom.appendChild(document.createTextNode(functionDetail));
			//preEl.dom.innerHTML+=this.functions[i].ref.toString();
			var label=new liujing.Label();
			panel.add(label);
			/*var el=new Ext.Element(document.createElement("a"));
			el.dom.name=this.functions[i].name;
			panelEl.dom.appendChild(el.dom);
			panelEl.insertHtml('beforeEnd',this.functions[i].ref.toString());*/
			panel.render(containerEl);
			
			label.getEl().dom.appendChild(preEl.dom);
			var hr=document.createElement("hr");
			containerEl.dom.appendChild(hr);
		}
	};
	</script>
</head>
<body>



</body>
